import { ControllerFieldState } from 'react-hook-form';
import { FormControlProps } from 'tea-component';

export function getStatus({
  fieldState,
  isSubmitted,
  isValidating
}: {
  fieldState: ControllerFieldState;
  isSubmitted?: boolean;
  isValidating: boolean;
}): FormControlProps['status'] {
  if (isValidating) {
    return 'validating';
  }
  if (isSubmitted !== undefined) {
    if (!fieldState.isDirty && !isSubmitted) {
      return undefined;
    }
  }
  return fieldState.invalid ? 'error' : 'success';
}

export const validateNewContractName = async (value: string) => {
  if (!value || !value.length) {
    return '请输入合约名称';
  }
  const exp = /^[A-Za-z][a-zA-Z0-9_]{0,127}$/;
  if (!exp.test(value)) {
    return '请输入128位以内字母、数字、下划线组合，必须以字母开头';
  }
  return undefined;
};
